<template>
  <div>
    <el-input :value="$route.query.roleName" disabled></el-input>
    <!-- 树形节点 -->
    <el-tree
      :data="Treedata"
      :show-checkbox="true"
      :default-expand-all="true"
      node-key="id"
      ref="tree"
      :highlight-current="true"
      :props="defaultProps"
    >
    </el-tree>
    <el-button type="primary" style="margin:20px 0" @click="SaveroleAuth">保存</el-button>
    <el-button @click="$router.replace({name:'Role'})">取消</el-button>
  </div>
</template>
<script>
export default {
  data() {
    return {
      Treedata: [],
      defaultProps: {
          children: 'children',
          label: 'name'
      },
    };
  },
  created(){
   //获取角色的id
   const roleId = this.$route.params.id
   this.getPermissions();

  },
  methods: {
   //根据角色的id获取角色所有的权限
   async getPermissions(roleId){
    const result=await this.$API.permission.reqtoAssign(roleId);
    console.log(result,"根据角色id获取角色所有的权限");
    if(result.code==20000){
     this.Treedata=result.data.children;
     const checkedIds=this.getCheckedIds(this.Treedata);
     console.log(checkedIds,"11111111111");
     this.$refs.tree.setCheckedKeys(checkedIds);

    }

   },
   //得到所有选中的id列表
   getCheckedIds(auth,initarr=[]){
    return auth.reduce((pre,item)=>{
    if(item.select&&item.level===4){
     pre.push(item.id)
    } else if(item.children){
     this.getCheckedIds(item.children,initarr)
    }
    return pre
     
    },initarr)


   },
   //点击保存按钮触发的事件
   SaveroleAuth(){
     this.$message.error("报错了")

   }
  },
};
</script>
<style scoped>
</style>